<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

if (reqVar('azione')=='deldom') {
	$sSql = "DELETE FROM rgl_mod_rque_dom "
		."WHERE id_sez_rque = ".intval(reqVar('id'))." AND cod_dom_rque = '".reqVarAS('cd')."'";
	$objDB->query($sSql);
	$msgPag .= "domanda ".reqVar('cd')." elimibata<br>";
}
if (reqVar('azione') == 'elimina') {
	$msgPag = deleSez();
} 

$sqlSel = "SELECT s.*, m.cod_mod_rque, m.descr AS m_descr FROM rgl_mod_rque_sez s "
	." INNER JOIN rgl_mod_rque m ON m.id_mod_rque = s.id_mod_rque" 
	." WHERE id_sez_rque = ".intval(reqVar('id'));
$objDB->query($sqlSel,'selRec');
if (!$acSez = $objDB->fetch('selRec')) {
	backOnErr("var. id. '".reqVar('id')."' mancante o errato");
}
$_REQUEST = array_merge($acSez,$_REQUEST);

$sqlSel = "SELECT d.* FROM rgl_mod_rque_dom d "
		." WHERE d.id_sez_rque = ".intval(reqVar('id'))
		." ORDER BY d.seq, d.cod_dom_rque ";
$objDB->query($sqlSel,'selDom');

$aCamForm = array();
require("_inc/const.campiSezRque.php");

$sCmdPagR = '';
if (true) {
	$sCmdPagR .= "&nbsp;&nbsp;&nbsp;";
	$sCmdPagR .= "<a class='cmd1' href='domRqueIns.php?id=".reqVar('id')."&$sXSNQS' >nuova domanda</a>";
	$sCmdPagR .= "&nbsp;&nbsp;&nbsp;";
	$sCmdPagR .= "<a class='cmd1' href='sezRqueMod.php?id=".reqVar('id')."&$sXSNQS' >modifica</a>";
	if (true) {
		$sCmdPagR .= "&nbsp;&nbsp;&nbsp;";
		$sCmdPagR .= "<a class='cmd1' href='javascript:confElim();' >elimina</a>";
	}
}
$sCmdPagR .= cmdTorna();

testaHtml('Gestione modelli',$msgPag,'modl');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
  	function confElim() {
  		if (confirm("Confermi l'eliminazione ?")) {
  			document.location.href = "<?=$_SERVER['PHP_SELF']."?id=".reqVar('id')."&azione=elimina&".$sXSNQS?>";
  		}
  	}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdPagR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Sezione questionario</td>
   </tr>
   <tr>
      <td class="lab1" colspan='4' align='left'>Modello</td>
      <td class="txt1" colspan='4' align='left'><?=$acSez['cod_mod_rque']?></td>
      <td class="txt1" colspan='12' align='left'><?=$acSez['m_descr']?></td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm,'vis');
?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Domande</td>
   </tr>
   <tr>
      <td class="lab1" colspan='1' align='center'>Seq.</td>
      <td class="lab1" colspan='2' align='center'>Codice</td>
      <td class="lab1" colspan='15' align='center'>Domanda - Risposta</td>
      <td class="lab1" colspan='1' align='center'>Peso</td>
      <td class="lab1" colspan='1' align='center'>Punti</td>
   </tr>
<?
while ($acDom = $objDB->fetch('selDom')) {
	$aRisp = unserialize($acDom['risposte']);
?>
   <tr>
      <td class="txt1" colspan='1' align='right'><?=$acDom['seq']?></td>
      <td class="txt1" colspan='2' align='left'><?=$acDom['cod_dom_rque']?></td>
      <td class="txt1" colspan='15' align='left'>
      	<a class='txt1' href='domRqueMod.php?id=<?=reqvar('id')?>&cd=<?=$acDom['cod_dom_rque']?>&xsn=<?=XSN?>'>
      	<?=$acDom['domanda']?></a>
      </td>
      <td class="txt1" colspan='1' align='right'><?=$acDom['peso']?></td>
      <td class="txt1" colspan='1' align='center'>
			<a class='txt1' href='sezRqueVis.php?azione=deldom&id=<?=reqvar('id')?>&cd=<?=$acDom['cod_dom_rque']?>&xsn=<?=XSN?>'>
      	<img border='0' src='../../_img/elimina.gif'/></a>
		</td>
   </tr>
<?
	foreach($aRisp as $kX => $aX) {
?>
   <tr>
      <td class="txt1" colspan='1' align='right'>&nbsp;</td>
      <td class="txt1" colspan='2' align='left'>&nbsp;</td>
      <td class="txt1" colspan='15' align='left'><?=($kX+1).") ".$aX[0]?></td>
      <td class="txt1" colspan='1' align='right'>&nbsp;</td>
      <td class="txt1" colspan='1' align='right'><?=$aX[1]?></td>
   </tr>
<?	
	}
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         <? echo $sCmdPagR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function deleSez() {
	global $objDB;
	$msg = '';

	$sSql = "SELECT * FROM rgl_mod_rque_dom " .
			" WHERE id_sez_rque = ".reqVar('id');
	$objDB->exec($sSql);
	$aRow = $objDB->fetch();
	if ($aRow) {
		$msg .= _t("Cancellare prima le domande")."<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSql = "DELETE FROM rgl_mod_rque_sez " .
			" WHERE id_sez_rque = ".reqVar('id');
	$objDB->exec($sSql);
	
	header("Location: ".urlTorna("Sezione eliminata"));
	exit();
}
